<?php

namespace app\queue\redis;

use constant\RedisConstant;
use util\RedisTool;
use util\WorkWxTool;
use Webman\RedisQueue\Client;
use Webman\RedisQueue\Consumer;

class UpdateUserStatusQueue implements Consumer
{
    public $queue = RedisConstant::UpdateUserStatus;

    //消费
    public function consume($data)
    {
        $taskType = $data['taskType']; //任务类型、标识
        $taskSign = $data['taskSign']; //分派名称、标识、ID

        //派发配置
        $taskSetting = RedisConstant::TaskPrefix . $taskType . ':task:' . $taskSign;
        $setting = RedisTool::get_forever($taskSetting, 'config');
        //修改时同步分配队列
        $redisKey = RedisConstant::TaskPrefix . $taskType . ':user:' . RedisConstant::TaskAssignNormal . $taskSign;
        $type = RedisTool::type($redisKey, 'list');
        $userList = [];
        if ($type == 3) $userList = RedisTool::get_lRange($redisKey, 0, -1, 'list');
        elseif ($type == 4) $userList = RedisTool::get_zSet_desc($redisKey, 0, 1, 'list');
        if ($userList) {
            //修改前一天的休息以及值班
            foreach ($userList as &$user) {
                $user['is_rest'] = 0;
                $user['is_duty'] = 0;
            }
            Client::send(RedisConstant::TaskUserList, [
                'name' => RedisConstant::TaskPrefix . $taskType . ':user:',
                'sign' => $taskSign,
                'mode' => $setting['assignMode'],
                'duty' => $setting['assignDuty'],
                'data' => $userList
            ]);
        }
        Client::send(RedisConstant::UpdateUserStatus, [
            'taskType' => $taskType,
            'taskSign' => $taskSign,
        ], time() + 86400);
    }

    // 消费失败回调
    public function onConsumeFailure(\Throwable $e, $package)
    {
        WorkWxTool::ErrorMsg('更新用户信息失败', $e->getMessage() . '\n' . json_encode($package['data'], 256));
    }
}